import java.util.HashMap;
import java.util.Map;

public class Test {

    //模拟


    //数青蛙
    public int minNumberOfFrogs(String c){
        char[] croakOfFrogs = c.toCharArray();
        String t = "croak";
        int n = t.length();
        int[] hash = new int[n];

        Map<Character,Integer> index = new HashMap<>();

        for (int i = 0; i < n; i++) {
            index.put(t.charAt(i),i);
        }

        for (char ch:croakOfFrogs) {
            if (ch == t.charAt(0)){
                if(hash[n-1] !=0) hash[n-1] --;
                hash[0] ++;
            }else{
                int i = index.get(ch);
                if (hash[i-1] == 0) return -1;
                hash[i-1] --;
                hash[i] ++;
            }
        }
        for (int i = 0; i < n-1; i++) {
            if (hash[i] != 0){
                return -1;
            }
        }
        return hash[n-1];
    }


    //外观数列
    public String countAndSay(int n){
        String ret = "1";
        for (int i = 1; i < n; i++) {
            StringBuilder tmp = new StringBuilder();
            int len =  ret.length();
            for (int left = 0,right = 0;right<len;) {
                while(right <len && ret.charAt(left) == ret.charAt(right)) right++;

                tmp.append(Integer.toString(right-left));
                tmp.append(ret.charAt(left));
                left = right;
            }
            ret = tmp.toString();
        }
        return ret;
    }


    //N字形变换
    public String convert(String s, int numRows){
        if (numRows == 1) return s;
        StringBuilder ret = new StringBuilder();
        int d = 2*numRows-2,n= s.length();

        for (int i = 0; i < n; i+=d) {
            ret.append(s.charAt(i));
        }
        for (int i = 1; i <numRows-1 ; i++) {
            for (int j = i ,k =d-j ; j < n||k<n ; j+=d,k+=d) {
                if (j<n) ret.append(s.charAt(j));
                if (k<n) ret.append(s.charAt(k));
            }
        }
        for (int i = numRows-1; i < n; i+=d) {
            ret.append(s.charAt(i));
        }
        return ret.toString();
    }




    //提莫攻击
    public int findPoisonedDuration(int[] timeSeries, int duration){
        int ret = 0;
        for (int i = 1; i < timeSeries.length; i++) {
            int x = timeSeries[i] - timeSeries[i-1];
            if (x>duration) ret+=duration;
            else ret+=x;
        }
        return ret+duration;
    }



    //替换所有的问号
    public String modifyString(String ss){
        char[] s = ss.toCharArray();
        int n = s.length;

        for (int i = 0; i < n; i++) {
            if (s[i] == '?'){
                for( char ch = 'a';ch<='z';ch++){
                    if ((i == 0 || ch!= s[i-1]) && (i == n-1 || ch != s[i+1])){
                        s[i] = ch;
                        break;
                    }
                }
            }
        }
        return String.valueOf(s);
    }


}
